import scrapy
from mySpider.items import ItcastItem
from scrapy.utils.response import get_base_url
# from scrapy.utils.url import urljoin_rfc
# from scrapy.utils.url import urljoin_rfc


class ItcastSpider(scrapy.Spider):
    name = "itcast"
    allowed_domains = ["itcast.cn"]
    start_urls = ["http://www.itcast.cn/channel/teacher.shtml"]

    def parse(self, response):
        filename = "teacher.html"
        open(filename, 'wb').write(response.body)
        context = response.xpath('/html/head/title/text()')
        title = context.extract_first()
        baseUrl = get_base_url(response)
        print(title)
        print("baseUrl " + baseUrl)
        items = []
        print("item test ");
        for each in response.xpath("//div[@class='tea_txt tea_txt_cur']/ul/li"):
            item = ItcastItem()
            name = each.xpath("div/h3/text()").extract()
            title = each.xpath("div/h4/text()").extract()
            info = each.xpath("div/p/text()").extract()
            # url =  each.xpath("img/src()").extract() 
            # url = each.xpath('.//img/@src').extract_first()
            urlRel = each.xpath('img/@src').extract_first()
            # print("item name " +name + " ,title  " + title + ", info " + info  );
            item['name'] = name[0]
            item['title'] = title[0]
            item['info'] = "hello"
            # item['info'] = info[0]
            # print("item name " + item['name'] + " ,title  " + item['title'] + ", info " + item['info']  );
            print("item name " + item['name'] + " ,title  " + item['title']  );
            # url = urljoin_rfc(baseUrl, urlRel)
            url = response.urljoin(urlRel)
            print("url is: " + url  )
            item['file_urls'] = [url]
            item['image_urls'] = [url]
            items.append(item)
            yield item

        # for each in response.xpath("//div[@class='li_txt']"):
        #     item = ItcastItem()
            # name = each.xpath("h3/text()").extract()
            # title = each.xpath("h4/text()").extract()
            # info = each.xpath("p/text()").extract()
            
            # item['name'] = name[0]
            # item['title'] = title[0]
            # item['info'] = info[0]
            # #print("item name " + item['name'] + " ,title  " + item['title'] + ", info " + item['info']  );
            
        #pass
        # return items
